package com.xuecheng.media.controller;

import com.xuecheng.api.media.model.MediaApi;
import com.xuecheng.api.media.model.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.exception.ExceptionCast;

import com.xuecheng.media.common.utils.UAASecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
public class MediaController implements MediaApi {
    @Autowired
    private MediaService mediaService;
    private MediaDTO dto;

    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {
        //1.获得机构的id值
        Long companyId = UAASecurityUtil.getCompanyId();

        //2.将vo数据转换为dto数据
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
       // dto.setCompanyId(companyId);
        //3.调用service获得结果数据
        MediaDTO result = mediaService.createMedia(dto);
        return result;
    }

    @Override
    @PostMapping("/media/list")
    public PageVO queryMediaList( PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = UAASecurityUtil.getCompanyId();
       // Long companyId=1232141425l;
        if (ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        return mediaService.queryMediaList(params,model,companyId);
    }


    @Override
    @DeleteMapping("/media/{mediaId}")
    public void removeMedia(@PathVariable Long mediaId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        //Long companyId=1232141425l;
        mediaService.removeMediaById(mediaId,companyId);
    }
}
